/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package program.gui;

import java.awt.Desktop;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.TableColumn;
import program.beans.Bill;
import program.beans.Category;
import program.bl.PrintBillController;
import program.gui.model.ViewBillTableModel;
import program.gui.model.ViewBillTableRenderer;
import program.utilities.DateUtilities;
import program.utilities.StatisticUtilities;

/**
 *
 * Responsible: Patrick, Bernhard
 */
public class PrintBillDlg extends javax.swing.JFrame {

    private PrintBillController pbc = new PrintBillController();
    private ArrayList<Category> selected = new ArrayList<>();
    private ArrayList<Bill> bills;
    private ViewBillTableModel vttm;

    /**
     * Creates new form Rechnung_Drucken
     */
    public PrintBillDlg() {
        initComponents();
        try {
            bills = pbc.getBills();
            vttm = new ViewBillTableModel(bills);
            tbBills.setModel(vttm);
            dpFrom.setDate((DateUtilities.getMonthBegin()));
            dpSince.setDate((DateUtilities.getMonthEnd()));
            for (int i = 0; i < tbBills.getColumnModel().getColumnCount(); i++) {
                TableColumn tc = tbBills.getColumnModel().getColumn(i);
                tc.setCellRenderer(new ViewBillTableRenderer());
            }
        } catch (Exception ex) {
                        JOptionPane.showMessageDialog(null, ex);

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        groupCat = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        dpFrom = new org.jdesktop.swingx.JXDatePicker();
        dpSince = new org.jdesktop.swingx.JXDatePicker();
        jPanel2 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbBills = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new java.awt.BorderLayout(2, 0));

        jPanel1.setLayout(new java.awt.GridBagLayout());

        java.awt.GridBagLayout jPanel6Layout = new java.awt.GridBagLayout();
        jPanel6Layout.columnWidths = new int[] {5};
        jPanel6Layout.rowHeights = new int[] {5};
        jPanel6.setLayout(jPanel6Layout);

        jButton1.setText("Speichern");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSave(evt);
            }
        });
        jPanel6.add(jButton1, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 20, 10, 20);
        jPanel1.add(jPanel6, gridBagConstraints);

        java.awt.GridBagLayout jPanel5Layout = new java.awt.GridBagLayout();
        jPanel5Layout.columnWidths = new int[] {30};
        jPanel5Layout.rowHeights = new int[] {30};
        jPanel5.setLayout(jPanel5Layout);

        jButton2.setText("Abbrechen");
        jPanel5.add(jButton2, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 20, 10, 20);
        jPanel1.add(jPanel5, gridBagConstraints);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        jPanel8.setLayout(new java.awt.BorderLayout());

        jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder("Datum"));
        jPanel10.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Von: ");
        jPanel10.add(jLabel1, new java.awt.GridBagConstraints());

        jLabel2.setText("Bis:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jPanel10.add(jLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        jPanel10.add(dpFrom, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        jPanel10.add(dpSince, gridBagConstraints);

        jPanel8.add(jPanel10, java.awt.BorderLayout.PAGE_START);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Kategorien"));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        jButton3.setText("Auswählen");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onSelectCategories(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(jButton3, gridBagConstraints);

        jButton4.setText("Rechnungen anzeigen");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showBills(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel2.add(jButton4, gridBagConstraints);

        jPanel8.add(jPanel2, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel8, java.awt.BorderLayout.PAGE_START);

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder("Rechnungen"));
        jPanel9.setLayout(new java.awt.BorderLayout());

        tbBills.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbBills);

        jPanel9.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel9, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void onSave(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSave
        Date from = dpFrom.getDate();
        Date since = dpSince.getDate();
                if ((from != null && since != null)) {
            if (from.before(since)) {
        try {
            File stat = StatisticUtilities.createStatistic(bills, selected, from, since);
            Object[] options = {"Speichern", "Nur ansehen"};
            String text = "Möchten Sie die erstellte Statistik speichern oder nur ansehen?";
            String title = "Statisitk";

            int selected = JOptionPane.showOptionDialog(null, text, title, 0, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
            if (selected == 0) {
                JFileChooser jfc = new JFileChooser();
                jfc.setSelectedFile(new File("statistic.pdf"));
                jfc.setFileFilter(new FileNameExtensionFilter("PDF", ".pdf"));
                jfc.showSaveDialog(this);
                File f = jfc.getSelectedFile();
                if (!f.getName().endsWith(".pdf")) {
                    f = new File(f.getPath() + ".pdf");
                }
                System.out.println(f);
                stat.renameTo(f);
                int op = JOptionPane.showConfirmDialog(this, "Möchten Sie die Datei gleich öffnen?");
                if (op == JOptionPane.YES_OPTION) {
                    Desktop.getDesktop().open(f);
                }

            } else {
                Desktop.getDesktop().open(stat);
            }


        } catch (Exception ex) {
                      JOptionPane.showMessageDialog(null, ex);

        }
        } else {
                JOptionPane.showMessageDialog(this, "Sie müssen einen gültigen Zeitraum wählen!", "Error", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Sie müssen einen gültigen Zeitraum wählen!", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_onSave

    private void onSelectCategories(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onSelectCategories
        SelectCategroiesDlg scd = new SelectCategroiesDlg(null, true, pbc.getCategories(), selected);
        scd.setVisible(true);
        if (scd.isOk()) {
            selected.clear();
            selected.addAll(scd.getSelectedCats());

        }
    }//GEN-LAST:event_onSelectCategories

    private void showBills(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showBills
        Date from = dpFrom.getDate();
        Date since = dpSince.getDate();

        if (from != null && since != null) {
            if (selected.size() > 0) {
                try {
                    pbc.updateList(from, since, selected);
                    tbBills.updateUI();
                } catch (Exception ex) {
                                JOptionPane.showMessageDialog(null, ex);

                }
            } else {
                JOptionPane.showMessageDialog(this, "Sie müssen mindestens eine Kategorie auswählen!", "Fehler", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Wählen Sie bitte ein korrektes Datum aus!", "Fehler", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_showBills


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXDatePicker dpFrom;
    private org.jdesktop.swingx.JXDatePicker dpSince;
    private javax.swing.ButtonGroup groupCat;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbBills;
    // End of variables declaration//GEN-END:variables
}
